package com.zycfc.zsf.boot.mybatis.typehandler;

import org.apache.ibatis.type.*;
import org.springframework.util.*;
import java.sql.*;

public class LongArrayTypeHandler extends BaseTypeHandler<Long[]>
{
    private final String delimiter;
    
    public LongArrayTypeHandler() {
        this.delimiter = ",";
    }
    
    public LongArrayTypeHandler(final String delimiter) {
        this.delimiter = delimiter;
    }
    
    public void setNonNullParameter(final PreparedStatement ps, final int i, final Long[] parameter, final JdbcType jdbcType) throws SQLException {
        ps.setString(i, StringUtils.arrayToDelimitedString((Object[])parameter, this.delimiter));
    }
    
    private Long[] stringToLongArray(final String str) {
        Long[] res = null;
        final String[] array = StringUtils.tokenizeToStringArray(str, this.delimiter);
        if (null != array && array.length > 0) {
            res = new Long[array.length];
            for (int i = 0; i < array.length; ++i) {
                final String tmp = array[i];
                if (null != tmp && tmp.length() > 0) {
                    res[i] = Long.valueOf(tmp);
                }
            }
        }
        return res;
    }
    
    public Long[] getNullableResult(final ResultSet rs, final String columnName) throws SQLException {
        final String str = rs.getString(columnName);
        if (rs.wasNull()) {
            return null;
        }
        return this.stringToLongArray(str);
    }
    
    public Long[] getNullableResult(final ResultSet rs, final int columnIndex) throws SQLException {
        final String str = rs.getString(columnIndex);
        if (rs.wasNull()) {
            return null;
        }
        return this.stringToLongArray(str);
    }
    
    public Long[] getNullableResult(final CallableStatement cs, final int columnIndex) throws SQLException {
        final String str = cs.getString(columnIndex);
        if (cs.wasNull()) {
            return null;
        }
        return this.stringToLongArray(str);
    }
}
